/*
  ivl_set.c  -  implementation of point initalization functions

  This file is part of the IVL Library (LIBIVL).
  
  LIBIVL - library for interval computations 
  
  Copyright (c) 2007 Mateusz Paprocki
  
  The IVL Library is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License as published
  by the Free Software Foundation; either version 2.1 of the License, or
  (at your option) any later version.

  The IVL Library is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
  License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with the IVL Library; see the file LICENCE.  If not, write to
  the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
  Boston, MA 02110-1301, USA.
*/

#include "interval.h"

int ivl_set(ivl_ptr ivl, ivl_srcptr op) {
	int inexact = 0, inexact_left, inexact_right;

	if (DOM_UNDEFINED(op))
		ivl->domain = IVL_UNDEFINED;
	else {		
		inexact_left = mpfr_set(IVL_LEFT(ivl), IVL_LEFT(op), GMP_RNDD);
		inexact_right = mpfr_set(IVL_RIGHT(ivl), IVL_RIGHT(op), GMP_RNDU);
	
		IVL_CHECK(ivl, op->domain, inexact, inexact_left, inexact_right);							
	}

	return inexact;	
}

int ivl_set_u(ivl_ptr ivl, unsigned long op) {
	int inexact = 0, inexact_left, inexact_right;

	inexact_left = mpfr_set_ui(IVL_LEFT(ivl), op, GMP_RNDD);
	inexact_right = mpfr_set_ui(IVL_RIGHT(ivl), op, GMP_RNDU);
	
	IVL_CHECK(ivl, IVL_TOTAL, inexact, inexact_left, inexact_right);

	return inexact;	
}

int ivl_set_s(ivl_ptr ivl, long op) {
	int inexact = 0, inexact_left, inexact_right;

	inexact_left = mpfr_set_si(IVL_LEFT(ivl), op, GMP_RNDD);
	inexact_right = mpfr_set_si(IVL_RIGHT(ivl), op, GMP_RNDU);
	
	IVL_CHECK(ivl, IVL_TOTAL, inexact, inexact_left, inexact_right);

	return inexact;	
}

int ivl_set_d(ivl_ptr ivl, double op) {
	int inexact = 0, inexact_left, inexact_right;

	inexact_left = mpfr_set_d(IVL_LEFT(ivl), op, GMP_RNDD);
	inexact_right = mpfr_set_d(IVL_RIGHT(ivl), op, GMP_RNDU);
	
	IVL_CHECK(ivl, IVL_TOTAL, inexact, inexact_left, inexact_right);

	return inexact;	
}

int ivl_set_z(ivl_ptr ivl, mpz_srcptr op) {
	int inexact = 0, inexact_left, inexact_right;

	inexact_left = mpfr_set_z(IVL_LEFT(ivl), op, GMP_RNDD);
	inexact_right = mpfr_set_z(IVL_RIGHT(ivl), op, GMP_RNDU);
	
	IVL_CHECK(ivl, IVL_TOTAL, inexact, inexact_left, inexact_right);

	return inexact;		
}

int ivl_set_q(ivl_ptr ivl, mpq_srcptr op) {
	int inexact = 0, inexact_left, inexact_right;

	inexact_left = mpfr_set_q(IVL_LEFT(ivl), op, GMP_RNDD);
	inexact_right = mpfr_set_q(IVL_RIGHT(ivl), op, GMP_RNDU);
	
	IVL_CHECK(ivl, IVL_TOTAL, inexact, inexact_left, inexact_right);

	return inexact;		
}

int ivl_set_f(ivl_ptr ivl, mpfr_srcptr op) {	
	int inexact = 0, inexact_left, inexact_right;

	inexact_left = mpfr_set(IVL_LEFT(ivl), op, GMP_RNDD);
	inexact_right = mpfr_set(IVL_RIGHT(ivl), op, GMP_RNDU);
	
	IVL_CHECK(ivl, IVL_TOTAL, inexact, inexact_left, inexact_right);

	return inexact;
}

int ivl_set_str(ivl_ptr ivl, const char *str, int base) {
	int inexact = 0, inexact_left, inexact_right;

	inexact_left = mpfr_set_str(IVL_LEFT(ivl), str, base, GMP_RNDD);
	inexact_right = mpfr_set_str(IVL_RIGHT(ivl), str, base, GMP_RNDU);
	
	if ((inexact_left == -1) || (inexact_right == -1)) {
		ivl->domain = IVL_UNDEFINED;
		inexact = -1;
	} else
		IVL_CHECK(ivl, IVL_TOTAL, inexact, inexact_left, inexact_right);

	return inexact;
}

void ivl_set_inf(ivl_ptr ivl, long sign) {
	mpfr_set_inf(IVL_LEFT(ivl), sign);
	mpfr_set_inf(IVL_RIGHT(ivl), sign);
	
	ivl->domain = IVL_TOTAL;
}
